/**
 ********************************************************************************
 *
 *文件名称：
 *
 *文件日期：
 *
 *文件描述：
 *
 *文件版本：
 *
 *修改历史：
 *
 ********************************************************************************
 ***/

/*******************************	头文件包含区	**********************************/
#include "device_ctrl.h"
#include "module_control.h"
#include "sensor.h"
/******************************* 全局变量定义区 **********************************/

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
u8 led1RestartFlag = 0; //刚上电时触发渐变开关灯效果标志

void light_sw_init(void)
{
	if(reg.RW.lamp_init_status == 0)
	{
		
	}
	else
	{
		if((reg.RW.lamp_init_status & 0x000F) == 0x0005)
		{
			reg.RW.lamp[0].sw = 0;
			reg.RW.lamp[0].light = 0;
			led1RestartFlag = 2;
		}
		if((reg.RW.lamp_init_status & 0x00F0) == 0x0050)
		{
			reg.RW.lamp[0].sw = 1;
			reg.RW.lamp[0].light = 100;
			led1RestartFlag = 2;
			if((reg.RW.lamp_init_status & 0x000F) == 0x0005)
			{
				reg.RW.lamp[0].sw = 0;
				reg.RW.lamp[0].light = 0;
				led1RestartFlag = 2;
				LAMP1_RESET;
			}
		}
		if((reg.RW.lamp_init_status & 0x0F00) == 0x0500)
		{
			
		}
		if((reg.RW.lamp_init_status & 0xF000) == 0x5000)
		{
			
		}
	}
}





#if 0
void read_lamp_power(void)
{
	static uint8_t lamp_light, cmd,lamp_sw;
	static uint16_t delay_time = 0;
	static uint16_t lamp_hundred,lamp_ten;
	if(reg.W.detection_dimming == 1)
	{
		switch(cmd)
		{
			case 0:
				cmd = 1;
				lamp_light = reg.RW.lamp[0].light;
				lamp_sw =  reg.RW.lamp[0].sw;
				delay_time = 0;
				//reg.RW.lamp[0].light = 100;
				//break;
			case 1:
				delay_time ++;
				reg.RW.lamp[0].light = 100;
				reg.RW.lamp[0].sw = 1;
				if(delay_time >= 200) //400)
				{
					if(!reg.R.lamp_hundred)
					{
						reg.R.lamp_hundred = reg.R.p1;
						//data_flash_write(); //保存一次参数
					}	
					lamp_hundred = reg.R.p1;
					cmd = 2;
				}
				break;
			case 2:
				delay_time ++;
				reg.RW.lamp[0].light = 10;
				reg.RW.lamp[0].sw = 1;
				if(delay_time >= 400) //400)
				{
					cmd = 3;
					lamp_ten =  reg.R.p1;

					if((!reg.R.lamp_ten) && (lamp_hundred > (lamp_ten + 10)))
					{
						reg.R.lamp_ten = reg.R.p1;
					}
					
				}
				
				break;
			case 3:
				reg.RW.lamp[0].light = lamp_light;
				reg.RW.lamp[0].sw = lamp_sw;
				if( (lamp_hundred >= reg.R.lamp_hundred * 0.8) && (lamp_hundred <= reg.R.lamp_hundred * 1.2))
				{
					reg.R.alarm.Lamp_failure = 0;
				}
				else
				{
					reg.R.alarm.Lamp_failure = 1;
				}
				
				if(lamp_hundred < lamp_ten + 10)
				{
					reg.R.alarm.lamp1Dimming_bit = 1;
				}
				else{
					reg.R.alarm.lamp1Dimming_bit = 0;
				}

				cmd = 0;
				reg.W.detection_dimming = 2;
				data_flash_write(); //保存一次参数
				break;
			default:
      			break;
		}

	}
	else if(!reg.W.detection_dimming)
	{
		reg.R.alarm.Lamp_failure = 0;
		reg.R.alarm.lamp1Dimming_bit = 0;
		reg.R.lamp_hundred = 0;
		reg.R.lamp_ten = 0;
	}
}
#endif
extern const u16 *pLastAlarm;
POWER_VALUE power __attribute__((at(0x10001756)));
#define MEASURE_OFFSET  6
void read_lamp_power(void)
{
	static uint8_t delay_time = 0;
	static uint8_t lamp_light, cmd,lamp_sw;
	volatile uint16_t *p = NULL;
	
	if(reg.W.detection_dimming == 1)
	{
		if((delay_time == 0) && (cmd == 0))
		{
			lamp_light = reg.RW.lamp[0].light;
			lamp_sw =  reg.RW.lamp[0].sw;
			cmd = 1;
			//delay_time = 0;
		}

		delay_time ++;
		reg.RW.lamp[0].light = (cmd) * 10;
		reg.RW.lamp[0].sw = reg.RW.lamp[0].sw | 0x01;
		
		if (*pLastAlarm & 0x0216) //过压过载过流保护
		{
			reg.W.detection_dimming = 0;
		}

		if(delay_time >= 200) //400)
		{
			delay_time = 0;
			p = (uint16_t *)&power;
			*(p + cmd - 1) = reg.R.p1;
			//*(p + cmd) = redata;
			if(cmd == 10)
			{
				cmd = 0;
				
				reg.RW.lamp[0].light = lamp_light;
				reg.RW.lamp[0].sw = lamp_sw;
				reg.R.lamp_hundred = reg.R.p1;
				if(power.hundred_percent_power < power.ten_percent_power + 10)
				{
					reg.R.alarm.lamp1Dimming_bit = 1;
					memset(&power, 0, sizeof(power));
				}
				else
				{
					reg.R.alarm.lamp1Dimming_bit = 0;
				}
				reg.W.detection_dimming = 2;
				data_flash_write();
				return ;
			}
			cmd ++;
		}
	}
	else if(!reg.W.detection_dimming)
	{
		reg.R.alarm.Lamp_failure = 0;
		reg.R.alarm.lamp1Dimming_bit = 0;
		cmd = 0;
		delay_time = 0;
		memset(&power, 0, sizeof(power));
	}
}

uint16_t Theoretical_power = 0;

void lamp_state_ctrl(void)
{
	//开关控制
	static u8 count = 0, last0_light = 100, sw_0_flag = 0;
	static u16 detection_time = 0;
	volatile uint16_t *p = NULL;
	float offset_allowed; 

	static uint8_t flash_flag = 0;
	
	flash_flag = 0;
	if(reg.W.flash_data)
	{
		flash_flag = 1;
		flash_test();
	}
	
	if (++count < 3)
		return;
	count = 0;
	if (*pLastAlarm & 0x0216) //过压过载过流保护
	{
		reg.RW.lamp[0].sw = 0;
	}

	if ((reg.RW.lamp[0].sw == 0) && (led1RestartFlag == 2)) //如果是关灯
	{
		if (led1RestartFlag)
		{
			switch (sw_0_flag)
			{
			case 0:
				sw_0_flag = 1;
				break;
			case 1:
				if(flash_flag == 0)
					set_light_Lamps1(--last0_light);
				if(last0_light <= 30)
					LAMP1_RESET;
				if (last0_light == 0)
				{
					device_time.else_data_repota_time =  ((u32)reg.W.data_tx_time * 100) - device_info.random - 1000;
					//delay_ms(50);
					set_light_Lamps1(0);
					LAMP1_RESET;
					sw_0_flag = 2;
					set_pwm_cm0(2);
				}
			default:
				break;
			}
		}
	}
	else //如果是开灯
	{
		sw_0_flag = 0;
		set_pwm_cm0(6);
		//渐变开关灯
		if (last0_light != reg.RW.lamp[0].light)
		{
			(last0_light > reg.RW.lamp[0].light) ? last0_light-- : last0_light++;
			if(flash_flag == 0)
			{
				if(last0_light < 10)
				{
					set_light_Lamps1(10);
				}
				else
				{
					set_light_Lamps1(last0_light);
				}
			}
			last0_light ? LAMP1_SET : LAMP1_RESET;
			device_time.else_data_repota_time =  ((u32)reg.W.data_tx_time * 100) - device_info.random - 1000;
		}
		else
		{
			last0_light = reg.RW.lamp[0].light;
			//刚上电的时候默认开灯，之后渐变灭灯运行到这里，然后执行该语句渐变开灯到最亮
			if (led1RestartFlag == 0)
			{
				led1RestartFlag = 1;
				reg.RW.lamp[0].sw = 1;
				reg.RW.lamp[0].light = 100;
			}
			if (last0_light == 100 && led1RestartFlag == 1)
			{
				led1RestartFlag = 2;
				sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
				//lamp_hundred = reg.R.p1;//detection_dimming((uint16_t *)&reg.R.lamp_hundred);
			}

			if(2 == reg.W.detection_dimming)
			{
				if(power.hundred_percent_power < power.ten_percent_power + 10)
				{
					reg.R.alarm.lamp1Dimming_bit = 1;
				}
				else if(reg.R.alarm.Lamp1_bit || reg.R.alarm.Lamp_failure)
				{
					reg.R.alarm.lamp1Dimming_bit = 1;
				}
				else
				{
					if(++detection_time >= 1500)
					{
						detection_time = 0;
						p = (uint16_t *)&power;

						if(reg.RW.lamp[0].light >= 10 )
							Theoretical_power = (*(p + (reg.RW.lamp[0].light / 10)) - *(p + (reg.RW.lamp[0].light / 10 - 1)) ) * (reg.RW.lamp[0].light % 10)  + *(p + (reg.RW.lamp[0].light / 10 - 1)) *10;
						else
							Theoretical_power = 10 * reg.R.p1;

						offset_allowed = (float)(MEASURE_OFFSET)/reg.R.p1;
						
						if((10 * reg.R.p1 >= Theoretical_power * (1 - offset_allowed)) && (10 * reg.R.p1 <= Theoretical_power * (1 + offset_allowed))) //|| (abs(10 * reg.R.p1 - Theoretical_power) <= 30)))
						{
							reg.R.alarm.lamp1Dimming_bit = 0;
						}
						else if((10 * reg.R.p1 < (Theoretical_power * (1 - offset_allowed) - 20)) || (10 * reg.R.p1 > (Theoretical_power * (1 + offset_allowed) + 20)))
						{
							reg.R.alarm.lamp1Dimming_bit = 1;
						}

					}
				}
				
			}
		}
	}

	//所有参数清零，恢复出厂设置
	if (reg.W.all_param_clear)
	{
		reg.W.all_param_clear = 0;
		memset(&ALARM_R, 0, sizeof(ALARM_R));
		param_init();
		data_flash_write(); //保存一次参数
		NVIC_SystemReset(); //复位
	}

	//复位
	if (reg.W.restart)
	{
		reg.W.restart = 0;
		reg.R.v = 2200;
		data_flash_write(); //保存一次参数
		NVIC_SystemReset(); //复位
	}

	//电能数据清零
	if (reg.W.clear_energy)
	{
		reg.W.clear_energy = 0;
		reg.R.e1 = 0;
		reg.R.e2 = 0;
		data_flash_write(); //保存一次参数
	}

	//策略清除
	if (reg.W.scene_clear)
	{
		reg.W.scene_clear = 0;
		memset(reg.W.scene, 0, sizeof(reg.W.scene));
		memset(&compareStruct, 0, sizeof(compareStruct));
		memset(timestampTable, 0, sizeof(timestampTable));
		reg.R.scene_sum = 0;
		data_flash_write(); //保存一次参数
	}

	//路灯时间清零
	if (reg.W.lamp_time_clear)
	{
		reg.W.lamp_time_clear = 0;
		reg.R.lamp1_all_run_time = 0;
		reg.R.lamp1_run_time = 0;
		reg.R.lamp2_all_run_time = 0;
		reg.R.lamp2_run_time = 0;
		data_flash_write(); //保存一次参数
	}

	//模块时间清零
	if (reg.W.module_time_clear)
	{
		reg.W.module_time_clear = 0;
		reg.R.module_all_run_time = 0;
		reg.R.module_run_time = 0;
		data_flash_write(); //保存一次参数
	}
	read_lamp_power();
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
extern leakageDetection leakDete;
void led_state_check(void)
{
	/* */
	if(leakDete.cnt_time < 1000)
	{
		
		leakDete.cnt_time++;
		
		if(leakDete.cnt_time <= 80)
		{
				BLUE_RESET;
				RED_RESET;
		}
		else if(leakDete.cnt_time < 800 && leakDete.cnt_time >= 80)
		{
				if(reg.R.software_version >= 10)
				{
					BLUE_SET;
					RED_RESET;
				}
				else
				{
					BLUE_RESET;
					RED_SET;
				}
		}
		else if(leakDete.cnt_time >= 800)
		{
			
			if(leakDete.cnt_write_flag == 0)
			{
				reg.RW.restart_count = 0;
				data_flash_write();
				leakDete.cnt_write_flag = 1;
			}
			
			BLUE_RESET;
			RED_SET;
		}

		return;
	}
	
	if (reg.RW.ota_flag == OTA_READY) //有升级任务时
	{
		BLUE_RESET;
		led(15, RED, 15, 10, 1);
		return;
	}
	if (!global_control.error_code) //无错误码时蓝灯指示状态
	{
		if (!global_control.alarm_flag) //无错误码无告警时不亮红灯
		{
			RED_RESET;
		}
		else //无错误码有告警时亮红灯
		{
			RED_SET;
		}
		if (global_control.cmd_num > AT_RESTART && global_control.cmd_num <= AT_CGATT_GET) //启动成功
		{
			led(100, BLUE, 100, 50, 1); //每过1s，以周期为1s,，占空比为500的频率闪烁一次
		}
#if (PROJECT_TYPEDEF == NB)
		else if (global_control.cmd_num > AT_CGATT_GET && device_info.mqtt_state < NB_LWM2M_OK) //注册上网
#elif (PROJECT_TYPEDEF == NB_ONENET)
		else if (global_control.cmd_num > AT_CGATT_GET && device_info.mqtt_state < NB_LWM2M_OK) //注册上网
#elif (PROJECT_TYPEDEF == CAT1)
		else if (global_control.cmd_num > AT_CGATT_GET && device_info.mqtt_state == 0) //注册上网
#endif
		{
			led(50, BLUE, 50, 25, 1); //每过500ms，以周期为500ms,，占空比为250的频率闪烁一次
		}
#if (PROJECT_TYPEDEF == NB)
		else if (device_info.mqtt_state == NB_LWM2M_OK) //链接上平台
#elif (PROJECT_TYPEDEF == NB_ONENET)
		else if (device_info.mqtt_state == NB_LWM2M_OK)											//链接上平台
#elif (PROJECT_TYPEDEF == CAT1)
		else if (device_info.mqtt_state == 1)										   //链接上平台
#endif
		{
			led(9, BLUE, 9, 8, 1); //每过90ms，以周期为90ms,，占空比为80的频率闪烁一次
		}
		else //常亮
		{
			if (reg.R.software_version == 1)
			{
				RED_SET;
				BLUE_RESET;
			}
			else
			{
				BLUE_SET;
				RED_RESET;
			}
		}
	}
	else //有错误码时红灯指示状态
	{
		switch (global_control.error_code)
		{
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
		case AT_QCCID:
#else
		case AT_CPIN:
#endif
			BLUE_RESET;
			led(150, RED, 50, 25, 1);
			break;

		case AT_CSQ:
			BLUE_RESET;
			led(200, RED, 50, 25, 2);
			break;

#if (PROJECT_TYPEDEF == NB)
			// case AT_CGATT_GET:
#endif
		case AT_CREG:
			BLUE_RESET;
			led(250, RED, 50, 25, 3);
			break;
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
		case AT_NMSTATUS:
#else
		case AT_QMTSUB:
			BLUE_RESET;
			led(350, RED, 50, 25, 4);
			break;
#endif
		default:
			BLUE_RESET;
			led(400, RED, 50, 25, 5);
			break;
		}
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/

void led(uint16_t delay_time, LED_TYPE led_type, uint16_t cycle, uint16_t compare, uint16_t cnt)
{
	static uint16_t led_time = 0, led_cycle_time = 0, count = 0;
	led_time++;
	if (led_time >= delay_time)
	{
		led_cycle_time++;
		if (led_cycle_time < compare) //到达比较值点亮LED
		{
			if (led_type == BLUE)
			{
				BLUE_RESET;
			}
			else
			{
				RED_RESET;
			}
		}
		else if (led_cycle_time < cycle) //否则点灭LED
		{
			if (led_type == BLUE)
			{
				BLUE_SET;
			}
			else
			{
				RED_SET;
			}
		}
		else //一个小闪灯周期完成
		{
			led_cycle_time = 0;
			count++; //次数加1
			if (count >= cnt)
			{
				count = 0;
				led_time = 0; //如果达到闪灯次数时间清零，等待下一个周期
			}
		}
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void LAMP(uint16_t delay_time, uint16_t cycle, uint16_t compare, uint16_t cnt)
{
	static uint16_t led_cycle_time1 = 0;
	led_cycle_time1++;
	if (led_cycle_time1 < compare) //到达比较值点亮LED
	{
		LAMP1_SET;
		LAMP2_SET;
	}
	else if (led_cycle_time1 < cycle) //否则点灭LED
	{
		LAMP1_RESET;
		LAMP2_RESET;
	}
	else
	{
		led_cycle_time1 = 0;
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
uint16_t time_count;
void device_run_time(void)
{
	//static uint32_t time_count, save_time;
	static uint32_t save_time;
	
	time_count++;
	save_time++;
	if (time_count >= 6000)
	{
		time_count = 0;
		U32_PLUS(reg.R.module_run_time);
		U32_PLUS(reg.R.module_all_run_time);

		if (reg.RW.lamp[0].sw)
		{
			U32_PLUS(reg.R.lamp1_run_time);
			U32_PLUS(reg.R.lamp1_all_run_time);
		}
		else
		{
			reg.R.lamp1_run_time = 0;
		}
		if (reg.RW.lamp[1].sw)
		{
			U32_PLUS(reg.R.lamp2_run_time);
			U32_PLUS(reg.R.lamp2_all_run_time);
		}
		else
		{
			reg.R.lamp2_run_time = 0;
		}
	}

	if (save_time >= 6000 * 30)
	{
		save_time = 0;
		data_flash_write();
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void flash_test(void)
{
	//uint8_t sw = reg.RW.lamp[0].sw;
	static uint8_t flags = 0,LED_sw = 0, LED_light = 0, sw_flags = 1;
	static uint32_t interval_times = 0;
	
	if(++interval_times <= 50)
	{
		return;
	}	
	
	interval_times = 0;
	(reg.W.flash_data)--;
	
	if(!flags)
	{
		LED_sw	  = reg.RW.lamp[0].sw;
		LED_light = reg.RW.lamp[0].light;
		flags = 1;
		LOGD("start sw:%u, light:%u\r\n", LED_sw, LED_light);
	}
	else
	{
			LAMP1_SET;
			set_pwm_cm0(6);
			reg.RW.lamp[0].sw = 1;
			
			sw_flags = !sw_flags;
			if(sw_flags)
			{
				reg.RW.lamp[0].light = 90;
				set_light_Lamps1(90);
			}
				
			else
			{
				reg.RW.lamp[0].light = 10;
				set_light_Lamps1(10);
			}
				
//		reg.RW.lamp[0].sw = !reg.RW.lamp[0].sw;
//		LED_light = reg.RW.lamp[0].light = 0xFF;
		if(reg.W.flash_data  == 0)
		{
			reg.W.flash_data = 0;
			reg.RW.lamp[0].sw = LED_sw;
			reg.RW.lamp[0].light = LED_light;
			if(reg.RW.lamp[0].sw == LED_sw)
				set_light_Lamps1(reg.RW.lamp[0].light);
			LOGD("end sw:%u, light:%u\r\n", reg.RW.lamp[0].sw, reg.RW.lamp[0].light);
			flags = 0;
		}
	}
}

